<?php
/**
 * Created by PhpStorm.
 * User: Mmx
 * Date: 2021\06\07
 * Time: 16:30
 */

namespace app\backend\controller\v4;

use app\backend\validate\PaperValidate;
use app\common\models\Paper as PaperModel;
use think\exception\ValidateException;

class Paper extends BaseController
{
    /**
     * 年级列表
     *
     * @return \think\Response
     */
    public function index()
    {
        // 试卷表
        $where = input('title');
        //获取列表
        if ($where) {
            $list = PaperModel::order('sort', 'desc')
                ->where('title', 'like', "%$where%")
                ->where('status', 1)
                ->field('*')
                ->select()->toArray();
            //获取总条数
            $total = PaperModel::where('status', 1)->where('title', 'like', "%$where%")->count();
            foreach ($list as &$v) {
                $v['children'] = [];
            }
        } else {
            $list = PaperModel::order('sort', 'desc')
                ->where('status', 1)
                ->field('*')
                ->select()->toArray();
            $list = getTree($list);
            //获取总条数
            $total = PaperModel::where('status', 1)->count();
        }
        return sucessMsg(200, '获取成功！', ['list' => $list, 'total' => $total]);
    }

    /**
     * 创建显示信息
     *
     * @return \think\Response
     */
    public function before()
    {
        $model = new PaperModel();
        $data = PaperModel::where(['status' => 1, 'pid' => 0])->field('id,title')->order(['sort'=>'desc','id'=>'desc'])->select()->toArray();
        array_unshift($data, ['id' => 0, 'title' => '顶级分类']);
        return sucessMsg(200, '获取成功！', $data);
    }

    /**
     * 显示创建轮播表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $model = new PaperModel();
        $data = input('post.');
        try {
            validate(PaperValidate::class)->scene('create')->check($data);
            if ($model->save($data)) {
                return sucessMsg(200, '创建成功！');
            }
            return errMsg(500, '创建失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 获取修改信息
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function edit()
    {
        $model = new PaperModel();
        $id = input('id/d', 0);
        $data = $model->where('id', $id)->find();
        if (empty($data))
            return errMsg(422, '无效id');
        return sucessMsg(200, '获取成功！', $data);
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update()
    {
        $data = input('post.');
        try {
            validate(PaperValidate::class)->scene('update')->check($data);
            if (PaperModel::update($data, ['id' => $data['id']])) {
                return sucessMsg(200, '修改成功！');
            }
            return errMsg(500, '修改失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete()
    {
        $id = input('id/d', 0);
        $data = PaperModel::find($id);
        if (empty($data)) {
            return errMsg(400, '要删除的数据为空！');
        }
        if (PaperModel::destroy($id)) {
            return sucessMsg(200, '删除成功！');
        }
        return errMsg(500, '删除失败！');
    }
}